<template><div><h2 id="字段翻译" tabindex="-1"><a class="header-anchor" href="#字段翻译"><span>字段翻译</span></a></h2>
<p>Dcat Admin 中文文档 /</p>
<h2 id="表格字段翻译" tabindex="-1"><a class="header-anchor" href="#表格字段翻译"><span>表格字段翻译</span></a></h2>
<p>数据表格中所有使用到字段的地方都会自动读取语言包中的翻译。</p>
<blockquote>
<p>语言包的详细使用方法请参考<a href="https://learnku.com/docs/dcat-admin/1.x/basic-use/8127" target="_blank" rel="noopener noreferrer">多语言</a>。</p>
</blockquote>
<h3 id="语言包名称" tabindex="-1"><a class="header-anchor" href="#语言包名称"><span>语言包名称</span></a></h3>
<p>语言包名称需要与控制器名相对应，假如控制器名<code v-pre>UserProfileController</code>，则对应的语言包为<code v-pre>resources/lang/{当前语言}/user-profile.php</code>（需要转化为小写中划线风格）。</p>
<p>如果想要更改语言包的名称，可以通过下面两种方式进行更改</p>
<p>方式1</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Dcat<span class="token punctuation">\</span>Admin<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Controllers<span class="token punctuation">\</span>AdminController</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">UserController</span> <span class="token keyword">extends</span> <span class="token class-name">AdminController</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 指定翻译文件名称</span>
<span class="line">     * </span>
<span class="line">     * <span class="token keyword">@var</span> <span class="token class-name"><span class="token keyword">string</span></span> </span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">protected</span> <span class="token variable">$translation</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'user1'</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token operator">...</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>方式2</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Dcat<span class="token punctuation">\</span>Admin<span class="token punctuation">\</span>Admin</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Admin</span><span class="token operator">::</span><span class="token function">translation</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'user1'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="示例" tabindex="-1"><a class="header-anchor" href="#示例"><span>示例</span></a></h3>
<p>现在假设语言包<code v-pre>resources/lang/zh_CN/user-profile.php</code>内容如下：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'fields'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'name'</span>  <span class="token operator">=></span> <span class="token string single-quoted-string">'名称'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'age'</span>   <span class="token operator">=></span> <span class="token string single-quoted-string">'年龄'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'班级'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>控制器<code v-pre>UserProfileController</code>中设置的<code v-pre>Grid</code>字段会自动读取以上翻译：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// 不设置labael会自动读取语言包翻译</span></span>
<span class="line"><span class="token variable">$grid</span><span class="token operator">-></span><span class="token function">id</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$grid</span><span class="token operator">-></span><span class="token property">name</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$grid</span><span class="token operator">-></span><span class="token property">age</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$grid</span><span class="token operator">-></span><span class="token keyword">class</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$grid</span><span class="token operator">-></span><span class="token function">filter</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$filter</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$filter</span><span class="token operator">-></span><span class="token function">gt</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'age'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 上面代码等同于</span></span>
<span class="line"><span class="token variable">$grid</span><span class="token operator">-></span><span class="token function">name</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'名称'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$grid</span><span class="token operator">-></span><span class="token function">age</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'年龄'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 也可这样使用</span></span>
<span class="line"><span class="token variable">$grid</span><span class="token operator">-></span><span class="token function">id</span><span class="token punctuation">(</span><span class="token function">admin_trans_field</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'id'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$grid</span><span class="token operator">-></span><span class="token function">name</span><span class="token punctuation">(</span><span class="token function">admin_trans_field</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$grid</span><span class="token operator">-></span><span class="token function">age</span><span class="token punctuation">(</span><span class="token function">admin_trans_field</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'age'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="公共翻译" tabindex="-1"><a class="header-anchor" href="#公共翻译"><span>公共翻译</span></a></h3>
<p>当<code v-pre>admin_trans_field</code>函数找不到当前控制器中对指定字段的翻译时，会去<code v-pre>global.php</code>中查找。如果某些字段是很多数据表中都有的，可以把这些翻译写在<code v-pre>resources/lang/{当前语言}/global.php</code>文件中。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token comment">// 常用的字段放在 global.php 中可以所有控制器共用。</span></span>
<span class="line">    <span class="token string single-quoted-string">'fields'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'id'</span>         <span class="token operator">=></span> <span class="token string single-quoted-string">'ID'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'created_at'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'创建时间'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'updated_at'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'更新时间'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div></div></template>


